<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@page import="com.tss.util.*"%>
<%@page import="com.jedi.*"%>
<%@page import="java.util.*"%>
<%@include file="../global_var.jsp"%>
<%@include file="../check_login.jsp"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
User just_user = (User)session.getAttribute("LoginUser");
if (!just_user.getRoleId().equals(admin_role_id) && !just_user.isInitOperator()) {
	out.println(strNoPowerInfo); 
	return;
}
User user = (User)session.getAttribute("LoginUser");
PowerMgr powerMgr = new PowerMgr();
String cmd = ParamUtils.getParameter(request,"cmd","");
String role_id = ParamUtils.getParameter(request,"role_id","");
String role_name = ParamUtils.getParameter(request,"role_name","");
String show_order = ParamUtils.getParameter(request,"show_order","1");
String role_desc = ParamUtils.getParameter(request,"role_desc","");
String [] powerValueList = ParamUtils.getParameters(request,"powerlist");  
String strErr = "";
if (user != null && !user.getRoleId().equals(admin_role_id) && !user.isInitOperator()) strErr = "对不起，只有管理员才有此权限!";
if (cmd.trim().equals("insert")) {
	if (strErr.trim().equals("") && role_id.trim().equals("")) {
		strErr = "角色ID号不能为空";
	}
	if (strErr.trim().equals("") && !ParamUtils.isRegId(role_id)) {
		strErr = "角色ID号只能由文字母、下划线、数字组成!";
	}
	if (strErr.trim().equals("") && role_name.trim().equals("")) {
		strErr = "角色名称不能为空!";
	}
	if (strErr.trim().equals("")) {
		try {
			Integer.parseInt(show_order);
		}
		catch(Exception ex) {
			strErr = "顺序号必须为一整数!";
		}
	}
	if (strErr.trim().equals("")) {
		Role role = new Role(role_id);
		role.setShowOrder(Integer.parseInt(show_order));
		role.setRoleName(role_name);
		role.setRoleDesc(role_desc);
		role.insert(powerValueList);
		if (role.hasErr()) strErr = role.getErrHtml();
	}
}
%>
<html>
<head>
 <base href="<%=basePath%>">
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title></title>
<link href="style/main.css" rel="stylesheet" type="text/css">
<script language="javascript" src="js/common.js"></script>
<script language="javascript">
function chk_submit() {
	var oForm = document.operatorForm;
	if (oForm.role_id.value == "") {
		alert("角色ID号不能为空");
		oForm.role_id.focus();
		return false;
	}
	if (oForm.role_name.value == "") {
		alert("角色名称不能为空");
		oForm.role_name.focus();
		return false;
	}
	oForm.cmd.value = "insert";
	return true;
}
</script>
</head>
<body  leftmargin="0" topmargin="0" marginwidth="0" marginheight="0">
<table width="100%" border="0" cellpadding="0" cellspacing="0">
  <tr>
    <td >
      <table width="100%" border="0" cellspacing="0" cellpadding="0">
       
        <tr> 
          <td height="100" align="center" valign="top" style="padding-top:20px;padding-bottom:10px;">
            <form name="operatorForm" method="post" onsubmit="return chk_submit();">
            <table width="80%" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF">
            
              <%if (!strErr.trim().equals("")) {%>
              <tr> 
                <td width="17%" height="28" align="right" bgcolor="#858585"><font color="#FFFFFF">错误提示：</font></td>
                <td width="83%" bgcolor="#EDEDF0"><font color=red><%=strErr%></font>&nbsp;</td>
              </tr>
              <%}%>
              <tr> 
                <td width="17%" height="24" align="right" bgcolor="#858585"><font color="#FFFFFF">角色ID：</font></td>
                <td bgcolor="#D7D7DB"><input type="text" name="role_id" value="<%=role_id%>" size="15" maxlength="15" class="inputbox"></td>
              </tr>
              <tr> 
                <td height="24" align="right" bgcolor="#858585"><font color="#FFFFFF">角色名称：</font></td>
                <td bgcolor="#EDEDF0"><input type="text" name="role_name" value="<%=role_name%>" size="20" maxlength="25" class="inputbox"></td>
              </tr>
              <tr> 
                <td height="24" align="right" bgcolor="#858585"><font color="#FFFFFF">顺序号：</font></td>
                <td bgcolor="#D7D7DB"><input type="text" name="show_order" value="<%=show_order%>" size="20" maxlength="25" class="inputbox"></td>
              </tr>
              <tr> 
                <td height="24" align="right" bgcolor="#858585"><font color="#FFFFFF">角色描述：</font></td>
                <td bgcolor="#EDEDF0">
                  <input type="text" name="role_desc" value="<%=role_desc%>" size="40" maxlength="100" class="inputbox">
              <%
              List powerList = powerMgr.get("","group_order,power_order");
              if (powerList != null && powerList.size() >0) {
              	String groupName = "";
              	String bgColor = "#EDEDF0";
              %>
              <%
              	for(int i=0;i<powerList.size();i++) {
              		Power item = (Power)powerList.get(i);
              		if (!item.getGroupName().equals(groupName)) {
              			groupName = item.getGroupName();
              			if (bgColor.equals("#EDEDF0"))
              				bgColor = "#D7D7DB";
              			else
              				bgColor = "#EDEDF0";
              %>
                </td>
              </tr>
              <tr> 
                <td height="24" align="right" bgcolor="#858585"><font color="#FFFFFF"><%=item.getGroupName()%>：</font></td>
                <td bgcolor="<%=bgColor%>"> 
              <%
              		}
              %>
                  <input type="checkbox" name="powerlist" value="<%=item.getId()%>"<%if (ParamUtils.inArray(item.getId(),powerValueList)){%> checked<%}%>> <%=item.getPowerName()%>
              <%
              	}
              }
              %>
                </td>
              </tr>
              <tr> 
                <td height="24" align="right" bgcolor="#EDEDF0">&nbsp;</td>
                <td bgcolor="#EDEDF0">
                  <input type="submit" name="submit" value="提 交" class="inputButton"> 
                  <input type="reset" name="reset" value="重 写" class="inputButton"> 
                  <input type="hidden" name="cmd" value="">
                </td>
              </tr>
            
            </table>
              </form>
          </td>
        </tr>
      </table>
    </td>
  </tr>
</table>
<%if (cmd.equals("insert") && strErr.trim().equals("")){%>
<script language="javascript">
alert("记录添加成功，系统将返回首页！");
location.href = "user_list.jsp";
</script>
<%}%>
</body>
</html>